package com.project.gjzb.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.gjzb.dto.ParamDto;
import com.project.gjzb.entity.BusVehicle;
import com.project.gjzb.entity.TobjBusVehicle;
import com.project.gjzb.mapper.BusVehicleMapper;
import com.project.gjzb.mapper.TobjBusVehicleMapper;
import com.project.gjzb.service.BusVehicleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BusVehicleServiceImpl implements BusVehicleService {

    @Resource
    private BusVehicleMapper busVehicleMapper;
    @Resource
    private TobjBusVehicleMapper tobjBusVehicleMapper;

    @Override
    @DS("datacenter")
    public List<TobjBusVehicle> getList() {
        return tobjBusVehicleMapper.getList();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void saveList(List<TobjBusVehicle> list) {
        for (TobjBusVehicle entity : list) {
            tobjBusVehicleMapper.insertSelective(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteByDate(ParamDto paramDto) {
        tobjBusVehicleMapper.deleteByDate(paramDto);
    }

    @Override
    @DS("master")
    public List<BusVehicle> getByDate(String date) {
        Example example = new Example(TobjBusVehicle.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("rq", date);
        c.andEqualTo("isremove", 0);
        List<TobjBusVehicle> list = tobjBusVehicleMapper.selectByExample(example);
        List<BusVehicle> busVehicleList = new ArrayList<>();
        if (list.size() > 0) {
            Map<String, List<TobjBusVehicle>> maps = list.stream().collect(Collectors.groupingBy(TobjBusVehicle::getBusline_id));
            for (Map.Entry<String, List<TobjBusVehicle>> entry : maps.entrySet()) {
                List<TobjBusVehicle> tobjBusVehicleList = entry.getValue();
                BusVehicle busVehicle = new BusVehicle();
                busVehicle.setId(tobjBusVehicleList.get(0).getBuslinevehicle_id());
                busVehicle.setXlid(entry.getKey());
                for (TobjBusVehicle tobjBusVehicle : tobjBusVehicleList) {
                    if ("1".equals(tobjBusVehicle.getBustype())) {
                        busVehicle.setDxcpcs(tobjBusVehicle.getVehicleamount());
                    } else if ("2".equals(tobjBusVehicle.getBustype())) {
                        busVehicle.setZxcpcs(tobjBusVehicle.getVehicleamount());
                    } else if ("3".equals(tobjBusVehicle.getBustype())) {
                        busVehicle.setQxcpcs(tobjBusVehicle.getVehicleamount());
                    } else if ("4".equals(tobjBusVehicle.getBustype())) {
                        busVehicle.setSccpcs(tobjBusVehicle.getVehicleamount());
                    } else if ("5".equals(tobjBusVehicle.getBustype())) {
                        busVehicle.setJjcpcs(tobjBusVehicle.getVehicleamount());
                    }
                }
                busVehicle.setSfkt(tobjBusVehicleList.get(0).getIsaircondition());
                busVehicle.setSfwrsp(tobjBusVehicleList.get(0).getIsselfserviceticket());
                busVehicle.setZt("1");
                busVehicleList.add(busVehicle);
            }
        }
        return busVehicleList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteAll() {
        busVehicleMapper.deleteAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void insert(List<BusVehicle> list) {
        for (BusVehicle entity : list) {
            busVehicleMapper.insertSelective(entity);
        }
    }
}
